/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-06-28
 * Time: 22:56
 * To BE A Better Person-2020-3-2
 */
public class Heap {
    public int[] elem;
    int UsedSize;
    public Heap(){
        elem=new int[10];
       UsedSize=0;
    }
    public void CreateHeap(int[] arr){
        //move the arr element to elem
        for (int i = 0; i < elem.length; i++) {
            elem[i]=arr[i];
            UsedSize++;
        }
        for (int i = (UsedSize-1-1)/2; i > 0 ; i--) {
            shiftDown(i,UsedSize);
        }

    }
    private void shiftDown(int root,int len){
        int p=root;
        int c=root*2+1;
        while(c<len){
            //有右孩子，让c移到最大的下标处
            if(c+1 < len && elem[c] < elem[c+1]){
                c++;
            }
            if(elem[c] > elem[p]){
                Change(elem[c],elem[p]);
            }else{
                break;
            }
        }
    }
    private void Change(int a,int b){
        int temp=a;
        a=b;
        b=temp;
    }
    public void  show(){
        for (int i = 0; i < elem.length; i++) {
            System.out.println(elem[i]);
        }
    }
}
